/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.web.controller;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.lang.StringUtils;
import org.cx1.model.utils.SystemProperties;
import org.cx1.service.utils.UAgentInfo;
import org.cx1.web.utils.SessionUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.view.RedirectView;

/**
 *
 * @author ECH
 */
public abstract class BaseController extends AbstractController {
    
    protected FileItemFactory factory = new DiskFileItemFactory();
    protected ServletFileUpload upload = new ServletFileUpload(factory);
    
    public BaseController() {
        
    }
    
    protected Map<String, Object> getMultipartFormParams(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            List<FileItem> items = upload.parseRequest(request);
            Map<String, String> formfields = new HashMap<String, String>();
            List<FileItem> filefields = new ArrayList<FileItem>();

            for (FileItem item : items) {
                if (item.isFormField()) {
                    formfields.put(item.getFieldName(), item.getString());
                } else {
                    filefields.add(item);
                }
            }
            map.put("formfields", formfields);
            map.put("filefields", filefields);
        } 
        catch (FileUploadException ex) {
            ex.printStackTrace();
        }
        return map;
    }
    
    protected Map<String, Object> getFormParams(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, String> formfileds = new HashMap<String, String>();
        Enumeration<String> paramNames = request.getParameterNames();

        while (paramNames.hasMoreElements()) {
            String name = paramNames.nextElement();
            formfileds.put(name, request.getParameter(name));
        }
        map.put("formfields", formfileds);
        return map;
    }
    
    protected SystemProperties getSystemProperties() {
        SystemProperties props = new SystemProperties();
        
        //CX: build web root path
        StringBuilder rootPath = new StringBuilder();
        rootPath.append("http://")
            .append(SystemProperties.SYSTEM_HOST)
            .append(":")
            .append(SystemProperties.SYSTEM_PORT);        
        props.setRootPath(rootPath.toString());
        
        
        StringBuilder appPath = new StringBuilder();
        appPath.append("http://")
            .append(SystemProperties.SYSTEM_HOST)
            .append(":")
            .append(SystemProperties.SYSTEM_PORT);
        
        if (!StringUtils.isEmpty(SystemProperties.WEBAPP_CONTEXT)) {
            appPath.append("/")
                .append(SystemProperties.WEBAPP_CONTEXT);
        }
        props.setAppPath(appPath.toString());
        
        
        return props;
    }
    
    protected boolean isThisRequestCommingFromAMobileDevice(HttpServletRequest request){

        String userAgent = request.getHeader("User-Agent");
        String httpAccept = request.getHeader("Accept");

        UAgentInfo detector = new UAgentInfo(userAgent, httpAccept);

        if (detector.detectMobileQuick()) {
            return true;
        }
                     
        //CX: for mobile structure testing
        String m = request.getParameter("mobile");
        if (!StringUtils.isEmpty(m)) {
            return true;
        }
        
        return false;
    }
    
    /*
    protected ModelAndView handleRequestInternal( HttpServletRequest request,
                                                  HttpServletResponse response) throws Exception {
        ModelAndView mav = new ModelAndView();
        Map map = new HashMap();
        
        String userID = SessionUtils.getUserID(request.getSession());
        if (StringUtils.isEmpty(userID)) {
            mav.setView( new RedirectView("login.htm") );
            return mav;
        }
        
        int action = StringUtils.isEmpty( request.getParameter("a") ) ? 0 : Integer.parseInt(request.getParameter("a"));
        
        switch (action) {
            default: {
                break;
            }
        }        
        mav.addObject("model", map);
        return mav;
    }
     * 
     */
}
